import { NodeStatus } from "../../../common/Enum";
import BTDecorator from "../../Base/BTDecorator";
import BTNode from "../../Base/BTNode";
/** 重复节点 */
export default class DecoratorRepeater extends BTDecorator {
	//重复次数，默认无限
	private repeatCount = Infinity
	//当前重复次数
	private curCount = 0
	//是否在失败时终止
	private endOnFailure = false

	constructor(children: BTNode[], repeatCount: number = Infinity, endOnFailure: boolean = false) {
		super(children)
		this.repeatCount = repeatCount
		this.endOnFailure = endOnFailure
	}

	canExecute(): boolean {
		return this.curCount < this.repeatCount && (!this.endOnFailure || (this.endOnFailure && this.status !== NodeStatus.Failure))
	}

	onChildExecuted(status: NodeStatus): void {
		this.curCount++
		this.status = status
	}

	onStart(): void {
		super.onStart()
		this.curCount = 0
	}
}